var mysqlClient = require('../libs/mysqlClient');

function Service() {
    this.list = function(params, callback) {
        let type = params.type
        let sql = ''
        if(type === '日'){
            sql = "SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as time,sum(money) " +
                "money FROM t_recharge_log GROUP BY time order by time"
        }
        if(type === '周'){
            sql = "SELECT DATE_FORMAT(create_time,'%Y-%u') as time,sum(money) " +
                "money FROM t_recharge_log GROUP BY  time  order by time"
        }
        if(type === '月'){
            sql = "SELECT DATE_FORMAT(create_time,'%Y-%m') as time,sum(money) " +
                "money FROM t_recharge_log GROUP BY  time order by time "
        }
        if(type === '年'){
            sql = "SELECT DATE_FORMAT(create_time,'%Y') as time,sum(money) " +
                "money FROM t_recharge_log GROUP BY  time order by time "
        }
        mysqlClient.queryList(sql,[],callback)
    }
    this.retrieve = function(id, params, callback) {
        mysqlClient.getObject('t_group', { id: id }, callback);
    }
    this.create = function(params, callback) {
        mysqlClient.addObject('t_recharge_log', params, callback);
    }
    this.update = function(id, params, callback) {
        mysqlClient.getObject('t_group', { id: id }, function(data) {
            if (data) {
                mysqlClient.updateObject('t_group', params, { id: id }, callback);
            } else {
                callback({ error: '没有数据' });
            }
        });
    }
    this.delete = function(id, params, callback) {
        mysqlClient.deleteObject('t_user', { id: id }, callback);
    }
}
exports.service = Service;
